package com.alisls.shequtao.service.user.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alisls.shequtao.service.user.dto.UserDTO;
import com.alisls.shequtao.service.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户管理
 *
 * @author Ke Wang
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Log4j2
public class UserController {

    /**
     * 用户服务
     */
    private final UserService userService;

    /**
     * 根据用户标识查询用户
     */
    @ApiOperation(value = "查询用户", notes = "根据用户标识查询用户")
    @ApiImplicitParam(
            name = "id",
            required = true,
            paramType = "path",
            dataType = "Integer",
            example = "1234567890123456789"
    )
	@GetMapping("/getUserById/{id}")
    @SentinelResource(value = "getUserById", fallback = "getUserByIdFallback", blockHandler = "getUserByIdBlockHandler")
	public ResponseEntity<UserDTO> getUserById(@PathVariable Long id) {
		UserDTO userDTO = userService.getUser(id);
		return ResponseEntity.ok(userDTO);
	}

    /**
     * 根据用户名查询用户
     */
    @ApiOperation(value = "查询用户", notes = "根据用户名查询用户")
    @ApiImplicitParam(
            name = "username",
            required = true,
            paramType = "path",
            dataType = "String",
            example = "wangke"
    )
	@GetMapping("/getUserByUsername/{username}")
	public ResponseEntity<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUser(username);
        if (log.isInfoEnabled()) {
            log.info("根据用户名查询到用户信息为：{}", userDTO);
        }
        return ResponseEntity.ok(userDTO);
	}

    /**
     * 根据用户名查询用户（用户信息带有密码信息）
     */
    @ApiOperation(value = "查询用户", notes = "根据用户名查询用户（用户信息带有密码信息")
    @ApiImplicitParam(
            name = "username",
            required = true,
            paramType = "path",
            dataType = "String",
            example = "wangke"
    )
    @GetMapping("/getUserWithPwdByUsername/{username}")
    public ResponseEntity<UserDTO> getUserWithPwdByUsername(@PathVariable String username) {
        log.info("执行根据用户名查询用户信息（用户信息包含用户密码）");
        UserDTO userDTO = userService.getUserWithPwd(username);
        return ResponseEntity.ok(userDTO);
    }

    /**
     * 获取用户菜单权限
     * 1.用户本身的菜单权限
     * 2.用户所有角色对应的菜单权限
     * 3.用户所有角色对应权限组内的菜单权限
     * 4.用户所在用户组对应的菜单权限
     * @param username 用户名
     * @return 用户的菜单权限
     */
    @GetMapping("/getUserMenuPermissions/{username}")
    @ApiOperation(value = "获取用户菜单权限", notes = "修改用户信息")
    public ResponseEntity<UserDTO> getUserMenuPermissions(@PathVariable String username) {
        return ResponseEntity.ok(new UserDTO());
    }

	/**
	 * 保存用户
	 */
	@PostMapping("/save")
	@ApiOperation(value = "保存用户", notes = "保存用户信息")
	public ResponseEntity<UserDTO> saveUser(@RequestBody @Valid UserDTO userDTO) {
		userDTO = userService.save(userDTO);
		return ResponseEntity.ok(userDTO);
	}

    /**
     * 修改用户
     * @param userDTO
     */
    @ApiOperation(value = "修改用户", notes = "修改用户信息")
	@PutMapping("/update/{id}")
	public ResponseEntity<UserDTO> updateUser(@PathVariable Long id, @RequestBody @Valid UserDTO userDTO) {
        userDTO.setId(id);
        userDTO = userService.update(userDTO);
        return ResponseEntity.ok(userDTO);
    }

}
